	.text

/* Switch from current_thread to next_thread. Make next_thread
 * the current_thread, and set next_thread to 0.
 * Use eax as a temporary register; it is caller saved.
 */
	.globl thread_switch
thread_switch:
	/* YOUR CODE HERE */

    // 存储current_thread的相关状态
    // 继续执行时的eip设置为什么?
    // 设置为这个函数的返回地址
    pushl %ebp
    movl %esp, %ebp

    movl current_thread, %eax
    movl (%eax), %esp

    // now %esp points to the stack of current thread
    // save ret eip
    pushl 4(%ebp)
    movl (%ebp), %ebp
    pushal

    // current_thread saved
    
    // current_thread @ 0x8d2c
    // next_thread @ 0x8d30

    // change cur thread to next_thread
    movl next_thread, %eax
    movl %eax, current_thread
    /*
    movl $0x8d2c, %eax
    movl next_thread, (%eax)
    */

    // set next_thread to zero
    movl $0, next_thread
    
    // run current thread, which is now next thread
    movl current_thread, %eax
    movl (%eax), %esp

    popal

	ret				/* pop return address from stack */
